Throw exception for unknown field in SpecialListfiles::formatValue()
authorSiebrand Mazeland <s.mazeland@xs4all.nl>
Fri, 27 Dec 2013 17:02:40 +0000 (18:02 +0100)
committerHashar <hashar@free.fr>
Thu, 2 Jan 2014 09:49:21 +0000 (09:49 +0000)
Change-Id: I99fedfb28aaf9b09b5d9fee27020601f3c06e8d4

includes/specials/SpecialListfiles.php
tests/phpunit/includes/specials/SpecialListFilesTest.php [new file with mode: 0644]

index b518140..ba3e73f 100644 (file)
@@ -387,6 +387,20 @@ class ImageListPager extends TablePager {
                UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
        }
 
+       /**
+        * @param string $field
+        * @param string $value
+        * @return Message|string|int The return type depends on the value of $field:
+        *   - thumb: string
+        *   - img_timestamp: string
+        *   - img_name: string
+        *   - img_user_text: string
+        *   - img_size: string
+        *   - img_description: string
+        *   - count: int
+        *   - top: Message
+        * @throws MWException
+        */
        function formatValue( $field, $value ) {
                switch ( $field ) {
                        case 'thumb':
@@ -446,6 +460,8 @@ class ImageListPager extends TablePager {
                        case 'top':
                                // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
                                return $this->msg( 'listfiles-latestversion-' . $value );
+                       default:
+                               throw new MWException( "Unknown field '$field'" );
                }
        }
 
diff --git a/tests/phpunit/includes/specials/SpecialListFilesTest.php b/tests/phpunit/includes/specials/SpecialListFilesTest.php
new file mode 100644 (file)
index 0000000..2689236
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Test class for SpecialListFiles class.
+ *
+ * Copyright © 2013, Antoine Musso
+ * Copyright © 2013, Siebrand Mazeland
+ * Copyright © 2013, Wikimedia Foundation Inc.
+ *
+ */
+
+class SpecialListFilesTest extends MediaWikiTestCase {
+       /**
+        * @expectedException MWException
+        * @expectedExceptionMesage invalid_field
+        * @covers ImageListPager::formatValue
+        */
+       public function testFormatValuesThrowException() {
+               $page = new ImageListPager( RequestContext::getMain() );
+               $page->formatValue( 'invalid_field', 'invalid_value' );
+       }
+}